package cn.xm.lingshi.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.xm.lingshi.dao.CartDao;
import cn.xm.lingshi.dao.OrderDao;
import cn.xm.lingshi.pojo.DingDan;
import cn.xm.lingshi.pojo.GouWuChe;
import cn.xm.lingshi.pojo.Shop;
import cn.xm.lingshi.utlis.BaseDao;

public class CartDaoImpl extends BaseDao implements CartDao {

	@Override
	public GouWuChe queryRecord(int offer, int pageSize , int userid
			) throws Exception {
		StringBuffer preparedSql = new StringBuffer();
		List<Object> listParams = new ArrayList<Object>();
		preparedSql
				.append(" SELECT cartid,userid,money FROM `carts` WHERE userid=? ");
		listParams.add(userid);
		
		Object[] params = listParams.toArray();
		ResultSet rs = super.executeQuery(preparedSql.toString(), params);
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT shopnum,shopname,shopimg,shoporice,shopid,brandname,kind FROM  cartdetails ");
		sql.append(" INNER JOIN `shop` ON `shop`.id=`cartdetails`.shopid ");
		sql.append(" INNER JOIN `kind` ON `kind`.kindid=`shop`.kindid ");
		sql.append(" INNER JOIN `pin` ON `pin`.brandid=`shop`.brandid ");
		sql.append(" WHERE cartid=? ");
		sql.append(" ORDER BY cartid LIMIT ?,?  ");
		GouWuChe order = new GouWuChe();
		List<Shop> arrList=new ArrayList<Shop>();
		if (rs != null && rs.next()) {
			order.setCartid(rs.getInt("cartid"));
			order.setMoney(rs.getDouble("money"));
			order.setUserid(rs.getInt("userid"));
			// 读取数据库，将订单id为1的商品
			if(rs.getString("cartid")!=null&&rs.getString("cartid")!=""){
				Object[] param={rs.getInt("cartid"),offer,pageSize};
				ResultSet set = super.executeQuery(sql.toString(), param);
				while (set != null && set.next()) {
					Shop shop=new Shop();
					shop.setKucun(set.getInt(1));
					shop.setShopname(set.getString(2));
					shop.setShopimg(set.getString(3));
					shop.setShoporice(set.getInt(4));
					shop.setId(set.getInt(5));
					shop.setBrand(set.getString("brandname"));
					shop.setKind(set.getString("kind"));
					arrList.add(shop);
				}
				order.setList(arrList);
			}
		}
		return order;
	}

	@Override
	public List<Shop> queryShopByorder(int orderid) throws Exception {
		String sql = "SELECT shopid,shopnum FROM `orderdetail` WHERE orderid=1";

		return null;
	}

	@Override
	public int delCartByCartid(int userid) throws Exception {
		String sql="DELETE FROM carts WHERE userid = ?";
		Object[] params={userid};
		return super.executeUpdate(sql, params);
	}

	@Override
	public int delCartdetailByCartid(int cartid) throws Exception {
		String sql="DELETE FROM cartdetails WHERE cartid = ?";
		Object[] params={cartid};
		return super.executeUpdate(sql, params);
	}
	
}
